<?php
function invoicelist()
{
    include tiremanager_plugin_dir_path . 'formsconfig.php';
    include tiremanager_plugin_dir_path . 'sellers_brands.php';
    global $wpdb;
    global $wp;
    $hereurl=home_url( $wp->request );
    $u = wp_get_current_user();
    $username = $u->user_login;
    $invoice_table = $wpdb->prefix . $tire_table_werehouse_invoices_prefix_name;
    $seller_list = array();
    $row = 0;
    foreach ($sellers_array as $seller => $value) {
        array_push($seller_list, $seller);
    }
    if (!current_user_can('level_2') and !in_array($username, $seller_list)) {
        die("نه");
    }
    if (isset($_GET['delete_invoice_id'])){
        if (current_user_can('level_2')){
            $del_id=tire_get_url_variable('delete_invoice_id');
            $wpdb->delete($invoice_table,array('id'=>$del_id));
            die("<h1>حذف شد</h1>");
        }
    }



    $query = "
    select * from $invoice_table where `tire_seller`='$username' 
 ORDER BY `date_updated` DESC
;
    ";


    if (current_user_can('level_2')) {
        $query = "select * from $invoice_table  ORDER BY `date_updated` DESC ";
    }
    $invoices = $wpdb->get_results($query);

    ?>
    <link rel="stylesheet" type="text/css"
          href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css"/>
    <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.4.1.min.js"></script>

    <script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
    <table id="tiretable" class="display" style="width:100%">
        <thead>
        <tr>
            <th>ردیف</th>
            <?php if (current_user_can('level_2')) { ?>
                <th>عامل</th>
            <?php } ?>
            <th>شماره فاکتور</th>
            <th>تاریخ فاکتور</th>
            <th>تصویرفاکتور</th>
            <?php if (current_user_can('level_2')) {
                ?>
                <th>حذف</th>
            <?php } ?>

        </tr>
        </thead>
        <tbody>
    <?php
    foreach ($invoices as $invoice) {
        echo "<tr>";
        echo "<td>";
        echo ++$row;
        echo "</td>";
        if (current_user_can('level_2')) {
            echo "<td>";
            echo $invoice->tire_seller;
            echo "</td>";
        }
        echo "<td>";
        echo $invoice->invoice_number;
        echo "</td>";
        echo "<td>";
        echo $invoice->invoice_date;
        echo "</td>";
        echo "<td>";
        if($invoice->invoice_date != NULL){
            $img=tire_save_invoice_path_url($invoice->invoice_image)['url'];
        echo "<a href=".$img.">
        تصویر
        </a>";
        };
        echo "</td>";
        if (current_user_can('level_2')) {
            echo "<th><a href=\"admin.php?page=invoicelist&delete_invoice_id=$invoice->id\">
حذف";
            echo "</td>";}


    echo "</th>";
     }
    ?>
        </tbody>
        <tfoot>
        <tr>
            <th>ردیف</th>
            <?php if (current_user_can('level_2')) { ?>
                <th>عامل</th>
            <?php } ?>
            <th>شماره فاکتور</th>
            <th>تاریخ فاکتور</th>
            <th>تصویرفاکتور</th>
            <?php if (current_user_can('level_2')) {
                ?>
                <th>حذف</th>
            <?php } ?>

        </tr>
        </tfoot>
    </table>
    <script>
        $(document).ready(function () {
                $('#tiretable').DataTable({
                        "pageLength": 100,
                        "language": {
                            "search": "جست و جو",
                            "Show": "نمایش",
                            "paginate": {
                                "first": "اول",
                                "last": "آخر",
                                "next": "بعدی",
                                "previous": "قبلی"
                            },
                            "info": "صفحه _PAGE_ از _PAGES_",
                            "lengthMenu": "نمایش _MENU_ مورد",

                        },

                    }
                )
            }
        );
    </script>


    <?php

}